package hsintiao.com.xintiaoAPP.util;

import java.text.SimpleDateFormat;
import java.util.Date;

//获取时间差的工具类(相差多少秒、小时、天，超过一周直接返回原本的时间按照格式yyyy-MM-dd)
public class   GetDifferTime {
    private static Long HH=1000*60*60L;//一小时
    private static Long MM=1000*60L;//一分钟
    private static Long DD=1000*60*60*24L;//一天
    private static Long WEEK=1000*60*60*24*7L;//一周

    public String getTime(Date before,Date now){
        long beforeTime = before.getTime();
        long nowTime = now.getTime();
        long xc=nowTime-beforeTime;
        if(0<xc&&xc<MM){
            System.out.println("相差"+xc+"秒");
            return String.valueOf(xc+"秒");
        }else if(MM<xc&&xc<HH){
            int xc1=(int)(xc/MM);
            System.out.println("相差"+xc1+"分钟");
            return String.valueOf(xc1+"分钟");
        }else if(HH<xc&&xc<DD){
            int xc1=(int)(xc/HH);
            System.out.println("相差"+xc1+"小时");
            return String.valueOf(xc1+"小时");
        }else if(DD<xc&&xc<WEEK){
            int xc1=(int)(xc/DD);
            System.out.println("相差"+xc1+"天");
            return String.valueOf(xc1+"天");
        }else {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            String dateTime = format.format(before);
            //System.out.println("日期="+dateTime);
            return dateTime;
        }
    }
}
